package cn.coldleaf.wiki.controllers;

import cn.coldleaf.wiki.pojo.dto.SpaceQueryDto;
import cn.coldleaf.wiki.pojo.dto.SpaceSaveDto;
import cn.coldleaf.wiki.pojo.vo.ResponseVo;
import cn.coldleaf.wiki.pojo.vo.SpaceVo;
import cn.coldleaf.wiki.services.SpaceService;
import cn.coldleaf.wiki.utils.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 空间操作Controller
 * @datetime 2023/10/20 上午11:29
 * @user cold
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("space")
@Api(tags = "文档空间操作接口")
public class SpaceController {
    private final SpaceService spaceService;

    /**
     * 创建文档空间
     * @datetime 2023-10-20 11:33:35
     * @user cold
     **/
    @PostMapping("save")
    @Operation(summary = "创建文档空间")
    public ResponseVo<?> saveSpace(@RequestBody SpaceSaveDto dto){
        if (spaceService.saveSpace(dto) > 0){
            return ResponseVo.success(null);
        }
        return ResponseVo.error(ErrorCode.SPACE_SAVE_ERROR);
    }

    /**
     * 修改文档空间
     * @datetime 2023-10-20 11:37:43
     * @user cold
     **/
    @PostMapping("modify")
    @Operation(summary = "修改文档空间")
    public ResponseVo<?> modifySpace(@RequestBody SpaceSaveDto dto){
        if (spaceService.modifySpace(dto) > 0){
            return ResponseVo.success(null);
        }
        return ResponseVo.error(ErrorCode.SPACE_MODIFY_ERROR);
    }

    /**
     * 删除文档空间
     * @datetime 2023-10-20 11:37:53
     * @user cold
     **/
    @PostMapping("remove")
    @Operation(summary = "删除文档空间")
    public ResponseVo<?> removeSpace(@RequestBody SpaceSaveDto dto){
        if (spaceService.removeSpace(dto) > 0){
            return ResponseVo.success(null);
        }
        return ResponseVo.error(ErrorCode.SPACE_REMOVE_ERROR);
    }

    /**
     * 查询空间列表
     * @datetime 2023-10-20 11:39:42
     * @user cold
     **/
    @GetMapping("searchList")
    @Operation(summary = "查询空间列表")
    public ResponseVo<List<SpaceVo>> searchList(SpaceQueryDto dto){
        List<SpaceVo> vos = spaceService.searchList(dto);
        return ResponseVo.success(vos);
    }

    /**
     * 获取指定空间
     * @datetime 2023-10-20 11:42:10
     * @user cold
     **/
    @GetMapping("get")
    @Operation(summary = "获取指定空间")
    public ResponseVo<SpaceVo> getSpace(SpaceQueryDto dto){
        SpaceVo vo = spaceService.getSpace(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.SPACE_NOT_EXISTS_ERROR);
    }
}
